<template>
  <div>
    <Top title="地址管理"></Top>
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd()"
      @edit="onEdit"
      @click-item="clickitem"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import "vant/es/toast/style";
import Top from "@/views/login/top";
import { getData } from "@/common/api/address";
import bus from "@/common/bus";
import {mapMutations} from "vuex"
export default {
  components: {
    Top,
  },
  data() {
    return {
      chosenAddressId: null,
      list: [],
      chooseAdress: false,
    };
  },
  methods: {
    ...mapMutations(['setOrderItem']),
    onAdd() {
      this.$router.push({ name: "newAdress", params: { text: "onAdd" } });
    },
    onEdit(item, index) {
      this.$router.push({
        name: "newAdress",
        params: { text: "onEdit", info: this.list[index] },
      });
    },
    clickitem(item) {
      if (this.chooseAdress) {
        this.setOrderItem(item)
        window.history.go(-1);

        return;
      }
    },
  },
  mounted() {
    getData().then((res) => {
      res.data.data.data.forEach((item) => {
        item.isDefault = item.isDefault == 0 ? false : true;
        item.address =
          item.province + item.city + item.county + " " + item.addressDetail;
        if (item.isDefault) {
          this.chosenAddressId = item.id;
        }
      });
      this.list = res.data.data.data;
    });
    if (this.$route.params.type == "cart") {
      this.chooseAdress = true;
    } else {
      if (this.$route.params.type == "my") {
        this.chooseAdress = false;
      }
    }
  },
  
};
</script>

<style scoped lang='scss'>
::v-deep .van-button--danger {
  background-color: #b0352f;
  border: none;
}
</style>